--[[
  通过Tuxedo传输的交易
]]
require("apm_core")
require("ApmXml")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local function parseIp(ip)
    local _, _, value = string.find(ip, "(%d+.%d+)$")
    return value == nil and "" or value
end

local arr_trans_code = {};
arr_trans_code["edzf01"] = "二代支付记账";
arr_trans_code["edzf02"] = "二代支付冲正";
arr_trans_code["edzf05"] = "二代支付对账";
arr_trans_code["edzf06"] = "二代支付账号查询";
arr_trans_code["edzf07"] = "二代支付批量账户查询";
arr_trans_code["edzf08"] = "二代支付账户明细查询";
arr_trans_code["edzf10"] = "二代支付同步机构";
arr_trans_code["edzf12"] = "二代支付交易状态查询";
arr_trans_code["edzf13"] = "二代支付信用卡账单明细查询";
arr_trans_code["edzf14"] = "二代支付信用卡还款核心记账";
arr_trans_code["edzf15"] = "二代支付信用卡还款核心记账";

--- main
function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 50007 then return end

    local s_em_type = ApmData.getMsgType()
    if s_em_type == nil then return end

    local s_trans_code
    local s_trans_name
    local s_rspcode
    local s_rspmsg

    if s_em_type == 1 then -- 响应
        -- 交易代码
        s_trans_code = ApmData.getField("request.dts.service.arg.108", 1)
        if s_trans_code ~= nil then
            s_trans_code = string.trim(s_trans_code)
            s_trans_name = arr_trans_code[s_trans_code];
            if s_trans_name == nil then s_trans_name = s_trans_code end
            ApmData.replaceField("transaction.type_code", s_trans_code)
            ApmData.replaceField("transaction.type_name", s_trans_name)
        end

        -- 交易返回码
        s_rspcode = ApmData.getField("response.dts.service.return.112", 1)
        if s_rspcode ~= nil then
            s_rspcode = string.trim(s_rspcode)
            ApmData.replaceField("response.code_string", s_rspcode);
            ApmData.delFields("response.code")
        end

        -- 交易返回信息
        s_rspmsg = ApmData.getField("response.dts.service.return.3247", 1)
        if s_rspmsg ~= nil then
            s_rspmsg = string.trim(s_rspmsg)
            ApmData.replaceField("response.error_message", s_rspmsg);
        end

        logInfo("Tuxedo(%s:%d->%s:%d) 交易名:%s|返回码:%s|%s",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            ApmData.getDstIp(),
            ApmData.getDstPort(),
            s_trans_name,
            s_rspcode,
            s_rspmsg)
    end

    return;
end
